package com.zjgsu.nftplatform.controller;
/*
 * @author jChen
 * @date 2022/4/21 10:52
 * @detail:
 */

import com.zjgsu.nftplatform.pojo.Result;
import com.zjgsu.nftplatform.pojo.ResultCode;
import com.zjgsu.nftplatform.service.FileUploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Api(tags = "文件上传")
@RestController
@RequestMapping("/file")
public class FileUploadController {
    private FileUploadService fileUploadService;

    @ApiOperation("图片上传")
    @PostMapping("/picture")
    public Result pictureUpload(MultipartFile file) throws IOException {
        String url = fileUploadService.fileUpload(file);
        if (url != null){
            return new Result(ResultCode.SUCCESS, url);
        }else{
            return new Result(ResultCode.FAILED);
        }
    }

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Result fileUpload(@RequestBody MultipartFile file) {
        try {
            return new Result(ResultCode.SUCCESS, fileUploadService.upload(file));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @Autowired
    public void setFileUploadService(FileUploadService fileUploadService) {
        this.fileUploadService = fileUploadService;
    }
}
